<template>
  <el-dialog
    :title="!dataForm.workerId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="小程序openid" prop="openid">
      <el-input v-model="dataForm.openid" placeholder="小程序openid"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="username">
      <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="身份证号码" prop="idcardnumber">
      <el-input v-model="dataForm.idcardnumber" placeholder="身份证号码"></el-input>
    </el-form-item>
    <el-form-item label="身份证照片" prop="idcardnumberpic">
      <el-input v-model="dataForm.idcardnumberpic" placeholder="身份证照片"></el-input>
    </el-form-item>
    <el-form-item label="银行卡号" prop="bankcardnumber">
      <el-input v-model="dataForm.bankcardnumber" placeholder="银行卡号"></el-input>
    </el-form-item>
    <el-form-item label="银行名称" prop="bankname">
      <el-input v-model="dataForm.bankname" placeholder="银行名称"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          workerId: 0,
          openid: '',
          username: '',
          idcardnumber: '',
          idcardnumberpic: '',
          bankcardnumber: '',
          bankname: '',
          createTime: ''
        },
        dataRule: {
          openid: [
            { required: true, message: '小程序openid不能为空', trigger: 'blur' }
          ],
          username: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          idcardnumber: [
            { required: true, message: '身份证号码不能为空', trigger: 'blur' }
          ],
          idcardnumberpic: [
            { required: true, message: '身份证照片不能为空', trigger: 'blur' }
          ],
          bankcardnumber: [
            { required: true, message: '银行卡号不能为空', trigger: 'blur' }
          ],
          bankname: [
            { required: true, message: '银行名称不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.workerId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.workerId) {
            this.$http({
              url: this.$http.adornUrl(`/yst/worker/info/${this.dataForm.workerId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.openid = data.worker.openid
                this.dataForm.username = data.worker.username
                this.dataForm.idcardnumber = data.worker.idcardnumber
                this.dataForm.idcardnumberpic = data.worker.idcardnumberpic
                this.dataForm.bankcardnumber = data.worker.bankcardnumber
                this.dataForm.bankname = data.worker.bankname
                this.dataForm.createTime = data.worker.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/yst/worker/${!this.dataForm.workerId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'workerId': this.dataForm.workerId || undefined,
                'openid': this.dataForm.openid,
                'username': this.dataForm.username,
                'idcardnumber': this.dataForm.idcardnumber,
                'idcardnumberpic': this.dataForm.idcardnumberpic,
                'bankcardnumber': this.dataForm.bankcardnumber,
                'bankname': this.dataForm.bankname,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
